﻿using ERPSystem.Common.DTO;
using ERPSystem.Common.Domains.Delivergoods;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using ERPSystem.Common.Domains.SalesOrderCommom;
using ERPSystem.Enum.CommonErrorCodes;
using System.Xml.Linq;
using System.Reflection.Metadata;
using Aspose.Words;

namespace SalesManagementCommon.API.SalesManagementCommons.Shippingnotess
{
    public class ShippingnoteQueryHandler : IRequestHandler<ShippingnoteQueryCommand, ResultPage<ShippingnoteShowDTO>>
    {
        private readonly IBaseRepository<Shippingnote> shippingnoteRepository;
        private readonly IBaseRepository<SalesOrder> salesOrderRepository;
        private readonly IBaseRepository<SalesOrderMaterial> salesOrderMaterialRepository;

        public ShippingnoteQueryHandler(IBaseRepository<Shippingnote> shippingnoteRepository, IBaseRepository<SalesOrder> salesOrderRepository, IBaseRepository<SalesOrderMaterial> salesOrderMaterialRepository)
        {
            this.shippingnoteRepository = shippingnoteRepository;
            this.salesOrderRepository = salesOrderRepository;
            this.salesOrderMaterialRepository = salesOrderMaterialRepository;
        }

        /// <summary>
        /// 发货显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<ShippingnoteShowDTO>> Handle(ShippingnoteQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<ShippingnoteShowDTO>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PageCount = 0 };
            try
            {
                var shippings = await(await shippingnoteRepository.GetAll()).ToListAsync();
                var salesOrders = await (await salesOrderRepository.GetAll()).ToListAsync();
                var salesorderMaterials = await (await salesOrderMaterialRepository.GetAll()).ToListAsync();
                var list = (from a in shippings
                          join b in salesOrders on a.SalesOrderId equals b.SalesOrderId
                          join c in salesorderMaterials on b.ItemBianma equals c.ItemBianma
                          select new ShippingnoteShowDTO()
                          {
                              ShippingnoteId = a.ShippingnoteId,//发货主键
                              SalesOrderId = a.SalesOrderId,//销售订单号
                              Stash = a.Stash,//库位
                              Customername = a.Customername,//客户名称
                              SalesStaff = b.SalesStaff,//销售人员
                              DocumentDate=b.DocumentDate,//单据日期
                              MaterialName = c.MaterialName,//物料名称
                              Warehouseentry = a.Warehouseentry,
                              Deliverydate = a.Deliverydate,
                              Freight = a.Freight,
                              Handlingcharges = a.Handlingcharges,
                              ReturnNoticesId = a.ReturnNoticesId,
                              Numberpack = a.Numberpack,
                              Quantity = a.Quantity,
                              ShippingnoteRemark = a.ShippingnoteRemark,
                              SalesOrderMaterialId = c.SalesOrderMaterialId,
                              TypeSpecification = c.TypeSpecification,
                              Color = c.Color,
                              UnitPrice = c.UnitPrice,
                              Amount = c.Amount,
                              Numberofpackages = c.Numberofpackages,
                              Pattern = c.Pattern,
                              Quality = c.Quality,
                              Packing = c.Packing,
                              Remark = c.Remark,
                              XSDocumentNumber = b.DocumentNumber,
                              FHDocumentNumber = a.DocumentNumber,
                              BusinessStatus = b.BusinessStatus,
                              CustomerProfileName = b.CustomerProfileName,
                              ItemBianma = b.ItemBianma,
                              DocumentType=c.DocumentType
                          });
                if (!string.IsNullOrEmpty(request.XSDocumentNumber))//销售订单号
                {
                    list = list.Where(d=>d.XSDocumentNumber == request.XSDocumentNumber).ToList();
                }
                if (!string.IsNullOrEmpty(request.FHDocumentNumber))
                {
                    list = list.Where(d => d.FHDocumentNumber == request.FHDocumentNumber).ToList();
                }
                if (!string.IsNullOrEmpty(request.CustomerProfileName))//客户名称
                {
                    list = list.Where(d => d.CustomerProfileName == request.CustomerProfileName).ToList();
                }
                if (!string.IsNullOrEmpty(request.SalesStaff))//销售人员
                {
                    list = list.Where(d => d.SalesStaff == request.SalesStaff).ToList();
                }
                if (!string.IsNullOrEmpty(request.ItemName))//物料名称
                {
                    list = list.Where(d => d.MaterialName == request.ItemName).ToList();
                }
                //if (request.StartTime != null)
                //{
                //    list = list.Where(d => d.Deliverydate >= request.StartTime).ToList();
                //}
                //if (request.EndTime != null)
                //{
                //    list = list.Where(d => d.Deliverydate <= request.EndTime).ToList();
                //}
                result.TotalCount = list.Count();
                result.PageCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                list = list.OrderBy(d=>d.SalesOrderId).Skip((request.PageIndex - 1)*request.PageSize).Take(request.PageSize).ToList();
                result.Data = list.ToList();
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
